package com.ruoyi.secondvitim.controller;

import java.util.List;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.secondvitim.domain.QueForm;
import com.ruoyi.secondvitim.domain.QueFormDetail;
import com.ruoyi.secondvitim.service.IQueFormDetailService;
import com.ruoyi.secondvitim.service.IQueFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * 调查问卷名称Controller
 *
 * @author alonso_ecit
 * @date 2021-12-17
 */
@RestController
@RequestMapping("/secondVitim/queForm")
public class QueFormController extends BaseController
{
    @Autowired
    private IQueFormService queFormService;

    @Autowired
    private IQueFormDetailService queFormDetailService;

    /**
     * 查询调查问卷名称列表
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueForm queForm)
    {
        startPage();
        List<QueForm> list = queFormService.selectQueFormList(queForm);
        return getDataTable(list);
    }

    /**
     * 导出调查问卷名称列表
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:export')")
    @Log(title = "调查问卷名称", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueForm queForm)
    {
        List<QueForm> list = queFormService.selectQueFormList(queForm);
        ExcelUtil<QueForm> util = new ExcelUtil<QueForm>(QueForm.class);
        util.exportExcel(response, list, "调查问卷名称数据");
    }

    /**
     * 获取调查问卷名称详细信息
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(queFormService.selectQueFormById(id));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:edit')")
    @Log(title = "调查文件状态更新", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody QueForm queForm)
    {
        return toAjax(queFormService.updateQueForm(queForm));
    }

    /**
     * 新增调查问卷名称
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:add')")
    @Log(title = "调查问卷名称", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QueForm queForm)
    {
        return toAjax(queFormService.insertQueForm(queForm));
    }

    /**
     * 修改调查问卷名称
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:edit')")
    @Log(title = "调查问卷名称", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QueForm queForm)
    {
        return toAjax(queFormService.updateQueForm(queForm));
    }

    /**
     * 修改调查问卷名称
     */
    @GetMapping("/preview/{id}")
    public String preview(@PathVariable("id") Long id, ModelMap mmap)
    {
//        QueForm queForm = queFormService.selectQueFormById(id);
        mmap.put("id", id);
        return "/preview";
    }

    /**
     * 问卷预览
     */
    @PostMapping("/preview")
    @ResponseBody
    public AjaxResult preview(Long id)
    {
        QueForm queForm = queFormService.selectQueFormById(id);
        QueFormDetail queFormDetail = new QueFormDetail();
        queFormDetail.setFormId(id);
        queFormDetail.setStatus("0");
        List<QueFormDetail> detailList = queFormDetailService.selectQueFormDetailList(queFormDetail);
        queForm.setQueFormDetailList(detailList);
        return AjaxResult.success(queForm);
    }

    /**
     * 删除调查问卷名称
     */
    @PreAuthorize("@ss.hasPermi('secondVitim:queForm:remove')")
    @Log(title = "调查问卷名称", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(queFormService.deleteQueFormById(id));
    }
}
